package com.tanhua.dubbo.api.impl.mongo;

import com.tanhua.domain.mongo.Sound;
import com.tanhua.domain.mongo.SoundScore;
import com.tanhua.dubbo.api.mongo.SoundApi;
import com.tanhua.dubbo.utils.IdService;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Service
@org.springframework.stereotype.Service
public class SoundApiImpl implements SoundApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private IdService idService;

    @Override
    public void save(Sound sound) {
        sound.setCreated(System.currentTimeMillis());

        //将pid放入表中
        sound.setSid(idService.getNextId("tanhua_sound"));

        //保存表
        mongoTemplate.save(sound);
    }

    //更新sound中的taskId
    @Override
    public void update(String id, String taskId) {
        Query query = new Query(Criteria.where("_id").is(new ObjectId(id)));
        Update update = new Update();
        update.set("taskId", taskId);
        mongoTemplate.updateFirst(query, update, Sound.class);
    }

    @Override
    public List<Sound> findTaskIdByCreated() {
        Query query = new Query(Criteria.where("state").is(0));
        query.with(Sort.by(Sort.Order.asc("created"))).limit(10);
        List<Sound> soundList = mongoTemplate.find(query, Sound.class);
        return soundList;
    }

    //根据taskId设置查询的结果
    @Override
    public void updateList(List<String> resultList,Integer result) {
        Query query = new Query(Criteria.where("taskId").in(resultList));
        Update update = new Update();
        update.set("state", result);
        mongoTemplate.updateFirst(query, update, Sound.class);
    }

    //根据用户id查询需要读取的语音
    @Override
    public Sound findOneSound(Long userId) {
        //查询出该用户的读取语音的情况
        Query query = new Query(Criteria.where("userId").is(userId));
        List<SoundScore> soundScoreList = mongoTemplate.find(query, SoundScore.class);

        //根据用户读取语音的情况找出最近的未听过的语音
        List<String> taskIdList = new ArrayList<>();
        for (SoundScore soundScore : soundScoreList) {
            String taskId = soundScore.getTaskId();
            taskIdList.add(taskId);
        }

        query = new Query(
                Criteria.where("state").is(1)
                        .and("userId").ne(userId)
                        .and("taskId").nin(taskIdList)
        );
        query.with(Sort.by(Sort.Order.desc("create"))).limit(1);

        Sound sound = mongoTemplate.findOne(query, Sound.class);

        //保存该用户的听取情况于用户观看音频表
        if(sound!=null) {
            SoundScore soundScore = new SoundScore();
            soundScore.setUserId(userId);
            soundScore.setTaskId(sound.getTaskId());
            soundScore.setCreated(System.currentTimeMillis());
            mongoTemplate.save(soundScore);
        }

        //听完即删除
        Query query1 = new Query(Criteria.where("taskId").is(sound.getTaskId()));
        mongoTemplate.remove(query1,Sound.class);

        return sound;
    }
}
